package com.home.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.home.demo.common.PageUtils;
import com.home.demo.common.Query;
import com.home.demo.dao.RoleDao;
import com.home.demo.entity.MenuAndRoleEntity;
import com.home.demo.entity.RoleEntity;
import com.home.demo.entity.UserAndRoleEntity;
import com.home.demo.entity.dto.role.RoleInfoDTO;
import com.home.demo.entity.vo.roleAndMenu.RoleAndMenuVO;
import com.home.demo.service.MenuAndRoleService;
import com.home.demo.service.RoleService;
import com.home.demo.service.UserAndRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {
    @Autowired
    private UserAndRoleService userAndRoleService;
    @Autowired
    private MenuAndRoleService menuAndRoleService;

    /**
     * 查看所有角色分页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoleEntity> page = this.page(
                new Query<RoleEntity>().getPage(params),
                new QueryWrapper<RoleEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 删除角色信息 同时删除用户跟菜单的关联
     * @param asList
     */
    @Override
    @Transactional
    public void removeByIdsInfo(List<String> asList) {

        this.removeByIds(asList);
        List<UserAndRoleEntity> userAndRoleEntity = userAndRoleService.list(new QueryWrapper<UserAndRoleEntity>().in("role_id", asList));
        if (!ObjectUtils.isEmpty(userAndRoleEntity)) {
            userAndRoleService.removeByIds(userAndRoleEntity);
        }
        List<MenuAndRoleEntity> menuAndRoleEntity = menuAndRoleService.list(new QueryWrapper<MenuAndRoleEntity>().in("role_id", asList));
        if (!ObjectUtils.isEmpty(menuAndRoleEntity)){
            menuAndRoleService.removeByIds(menuAndRoleEntity);
        }
    }

    /**
     * 获取角色详情
     * @param id
     * @return
     */
    @Override
    public RoleAndMenuVO getByIdInfo(String id) {
        RoleAndMenuVO byIdInfo = baseMapper.getByIdInfo(id);

        return byIdInfo;

    }

    @Override
    @Transactional
    public void saveInfo(RoleInfoDTO role) {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setRole(role.getRole());
        this.save(roleEntity);

        saveRoleAndMenu(roleEntity.getId(),Arrays.asList(role.getMenuIds()));
    }

    @Override
    @Transactional
    public void updateInfoById(RoleInfoDTO role) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtil.copyProperties(role,roleEntity);
        roleEntity.setId(role.getRoleId());
        this.updateById(roleEntity);

        menuAndRoleService.remove(new QueryWrapper<MenuAndRoleEntity>().eq("role_id",role.getRoleId()));

        saveRoleAndMenu(role.getRoleId(),Arrays.asList(role.getMenuIds()));

    }

    private void saveRoleAndMenu(String id,List<String> ids){
        List<MenuAndRoleEntity> entity1 = ids.stream().map(entity -> {
            MenuAndRoleEntity m = new MenuAndRoleEntity();
            m.setRoleId(id);
            m.setMenuId(entity);
            return m;
        }).collect(Collectors.toList());
        menuAndRoleService.saveBatch(entity1);
    }
}